package com.efounder.adapter;

import android.content.Context;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.efounder.bean.Address;
import com.efounder.mobilemall.R;
import com.efounder.utils.ResStringUtil;

import java.util.List;

/**
 * 新的地址选择列表
 * Created by will on 18-3-6.
 */

public class AddressListAdapter extends RecyclerView.Adapter<AddressListAdapter.AddressViewHolder>{

    private List<Address> addresses;
    private Context mContext;

//    private OnAddressSelectedListener onAddressSelectedListener;
    private OnShowEditAddressListener onShowEditListener;
    private OnDeleteAddressListener onDeleteAddressListener;

    public AddressListAdapter(List<Address> addresses, Context mContext) {
        this.addresses = addresses;
        this.mContext = mContext;
    }

    @Override
    public AddressViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext)
                .inflate(R.layout.item_address_list, parent, false);
        return new AddressViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final AddressViewHolder holder, final int position) {
        holder.tvName.setText(addresses.get(position).getUserName());
        String phone =addresses.get(position).getPhone();
        holder.tvPhone.setText(phone.substring(0,3)+"****"+phone.substring(7,phone.length()));
        holder.ivSelected.setVisibility(View.GONE);
        if (addresses.get(position).getIsSelected().equals("1")){
            holder.tvAddress.setText(ResStringUtil.getString(R.string.mail_frame_default) + addresses.get(position).getArea() + mContext.getResources().getString(R.string.mall_kong_ge)
                    + addresses.get(position).getAddress());
        }else {
            holder.tvAddress.setText(addresses.get(position).getArea() +mContext.getResources().getString(R.string.mall_kong_ge)+ addresses.get(position).getAddress());
        }
        holder.ivEdit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onShowEditListener.onShowEditPop(position);
            }
        });
        setViewHeight(holder.llAddress,holder.btDeletete);
        /**
         * 地址删除按钮的回调
         */
        holder.btDeletete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(onDeleteAddressListener!=null){
                    onDeleteAddressListener.onDeleteAddress(position);
                }
            }
        });
//        if (addresses.get(position).isCurrentSelected()){
//            holder.itemView.setBackground(ContextCompat.getDrawable(mContext, R.color.darkgray));
//            holder.ivSelected.setVisibility(View.VISIBLE);
//        }else {
//            holder.itemView.setBackground(ContextCompat.getDrawable(mContext, R.color.white));
//            holder.ivSelected.setVisibility(View.INVISIBLE);
//        }
//        holder.itemView.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                onAddressSelectedListener.onAddressSelected(holder.getAdapterPosition());
//            }
//        });
    }

    @Override
    public int getItemCount() {
        return addresses.size();
    }


    class AddressViewHolder extends RecyclerView.ViewHolder{

        ImageView ivEdit;
        ImageView ivSelected;
        TextView tvName;
        TextView tvPhone;
        TextView tvAddress;
        /**
         * 根布局
         */
        private LinearLayout llAddress;
        /**
         * 删除地址按钮
         */
        private Button btDeletete;
        public AddressViewHolder(View itemView) {
            super(itemView);
            tvName = (TextView) itemView.findViewById(R.id.tv_name);
            tvPhone = (TextView) itemView.findViewById(R.id.tv_phone);
            tvAddress = (TextView) itemView.findViewById(R.id.tv_address);
            ivEdit = (ImageView) itemView.findViewById(R.id.iv_edit_address);
            ivSelected = (ImageView) itemView.findViewById(R.id.iv_selected);
            btDeletete = itemView.findViewById(R.id.bt_delete);
            llAddress = itemView.findViewById(R.id.ll_address);
        }
    }

//    public interface OnAddressSelectedListener{
//        void onAddressSelected(int position);
//    }
//
//    public void setOnAddressSelectedListener(OnAddressSelectedListener onAddressSelectedListener) {
//        this.onAddressSelectedListener = onAddressSelectedListener;
//    }

    public interface OnShowEditAddressListener{
        void onShowEditPop(int position);
    }

    public void setOnShowEditListener(OnShowEditAddressListener onShowEditPopupListener) {
        this.onShowEditListener = onShowEditPopupListener;
    }
    public interface OnDeleteAddressListener{
        void onDeleteAddress(int position);
    }

    public void setDeleteAddressListener(OnDeleteAddressListener onDeleteAddressListener) {
        this.onDeleteAddressListener = onDeleteAddressListener;
    }

    /**
     * 动态计算View高度
     */
    public static void setViewHeight(final View targetView, final View view){
        targetView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                targetView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                int height = targetView.getMeasuredHeight();
                if(height!=0){
                    int viewHeight =  height;
                    ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
                    layoutParams.height = viewHeight;
                    layoutParams.width = view.getMeasuredWidth();
                    view.setLayoutParams(layoutParams);
                }
            }
        });

    }

}
